<?php
App::uses('AppModel', 'Model');
/**
 * SysLogisticTpl Model
 *
 * @property SysWebsites $SysWebsites
 * @property Provience $Provience
 * @property City $City
 * @property Area $Area
 */
class SysLogisticTpl extends AppModel {


	
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'price' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				'required' => true,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'SysWebsites' => array(
			'className' => 'SysWebsites',
			'foreignKey' => 'sys_websites_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'SysLogistic' => array(
				'className' => 'SysLogistic',
				'foreignKey' => 'sys_logistic_id',
				'conditions' => '',
				'fields' => '',
				'order' => ''
		),
		'Provience' => array(
			'className' => 'Areas.areas',
			'foreignKey' => 'provience_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'City' => array(
			'className' => 'Areas.areas',
			'foreignKey' => 'city_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Area' => array(
			'className' => 'Areas.areas',
			'foreignKey' => 'area_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	public function checkExist($data){
		$this->recursive = -1;
		$conditions = array(
				'SysLogisticTpl.sys_websites_id'=>$data['SysLogisticTpl']['sys_websites_id'],
				'SysLogisticTpl.sys_logistic_id'=>$data['SysLogisticTpl']['sys_logistic_id'],
				'SysLogisticTpl.provience_id'=>$data['SysLogisticTpl']['provience_id'],
				'SysLogisticTpl.city_id'=>$data['SysLogisticTpl']['city_id'],
				'SysLogisticTpl.area_id'=>$data['SysLogisticTpl']['area_id'],
		);
		if($data['SysLogisticTpl']['id']>0){
			$conditions['SysLogisticTpl.id <>'] = $data['SysLogisticTpl']['id'];
		}
		
		return $this->find('count',array(
			'conditions'=>$conditions
		));
	}
	
	public function getSLT($site_id,$p=0,$c=0,$a=0){
		return $this->find('first',array(
				'conditions'=>array(
						'SysLogisticTpl.sys_websites_id'=>$site_id,
						'SysLogisticTpl.provience_id'=>$p,
						'SysLogisticTpl.city_id'=>$c,
						'SysLogisticTpl.area_id'=>$a,
				)
		));
	}
}
